import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    dataList: [],
    dialogFormVisibleAdd: false,
    checkedRoles: [],
    roles: [],
    pagination: {
      pageSizes: [5, 10, 20, 50, 100],
      queryString: '',
      currentPage: 1,
      pageSize: 5,
      total: 0
    },
    formData: {
      rolename: '',
      password: '',
      password2: ''
    }
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/'
    }
    this.getDataList()
    this.getRoleList()
  },
  methods: {
    async addManager() {
      if (this.formData.password != this.formData.password2) {
        this.$message.error('两次密码不一致')
        return
      }
      if (this.checkedRoles.length == 0) {
        this.$message.error('请选择角色')
        return
      }
      if (this.formData.rolename == '') {
        this.$message.error('请输入用户名')
        return
      }
      if (this.formData.password == '') {
        this.$message.error('请输入密码')
        return
      }
      if (this.formData.password2 == '') {
        this.$message.error('请输入确认密码')
        return
      }
      const res = await request.post('/manage/addManager',{
        username: this.formData.rolename,
        password: this.formData.password,
        roleIds: this.checkedRoles
      })
      this.$message.success(res.msg)
      this.getDataList()
      this.dialogFormVisibleAdd = false
    },
    async getRoleList() {
      const res = await request.get('/role/getRoleList')
      this.roles = res.data
    },
    async getDataList() {
      const res = await request.get('/manage/getManagerPageList',{
        params: {
          queryString: this.pagination.queryString,
          currentPage: this.pagination.currentPage,
          pageSize: this.pagination.pageSize
        }
      })
      this.dataList = res.data.list
      this.pagination.total = res.data.pageTotal
    },
    findPage() {
      this.getDataList()
    },
    handleAddManager() {
      this.dialogFormVisibleAdd = true
    },
    async handleStatusChange(row) {
      const params = new URLSearchParams();
      params.append('id', row.id)
      params.append('isDelete', row.isDelete)
      const res = await request.put('/manage/changeIsDelete', params)
      this.$message.success(res.msg)
      this.getDataList()
    },
    handleSizeChange(size) {
      this.pagination.pageSize = size
      this.getDataList()
    },
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage
      this.getDataList()
    }
  }
})